﻿#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#include "GDShareView.h"

#include "../Util/GDUtil.h"
#include "../DataModule/ConfigDataModule.h"

GDShareView* GD_SHARE_VIEW = NULL;

GDShareView::GDShareView(bool closeToHidden)
{
	this->closeToHidden = closeToHidden;
}


GDShareView::~GDShareView(void)
{
}

GDShareView* GDShareView::createWithFrame(const CCRect& rect,bool closeToHidden){
	GDShareView *view = new GDShareView(closeToHidden);

	if (view && view->initWithFrame(rect)){
		view->initView();

		view->autorelease();
		return view;
	}

	return NULL;
}

GDShareView* GDShareView::createWithCenter(const CCRect& rect, bool closeToHidden){
	GDShareView *view = new GDShareView(closeToHidden);

	if (view && view->initWithCenter(rect)){
		view->initView();

		view->autorelease();
		return view;
	}

	return NULL;
}

void GDShareView::initView(){
	this->setColor(ccc4(0,0,0,56));
	CCSize winSize = this->getBounds().size;

	CAButton *closeBtn = CAButton::createWithFrame(CCRect(0, 0, winSize.width, winSize.height),CAButtonTypeCustom);
	closeBtn->addTarget(this, CAControl_selector(GDShareView::onCloseBtnClick), CAControlEventTouchUpInSide);
	this->addSubview(closeBtn);

	mainPanel = CAView::createWithFrame(CCRect(0,0,winSize.width,winSize.height));
	mainPanel->setColor(CAColor_white);
	this->addSubview(mainPanel);

	mainNode = CAView::createWithFrame(CCRect(_ds(26), _ds(18), winSize.width - _ds(26), mainPanel->getBounds().size.height - _ds(88)));
	mainPanel->addSubview(mainNode);

	initSharePlatform();

	closeNode = CAView::createWithFrame(CCRect(0, mainNode->getFrameOrigin().y + mainNode->getBounds().size.height+_ds(18), winSize.width, _ds(88)));
	closeNode->setColor(CAColor_gray);
	mainPanel->addSubview(closeNode);

	CAButton *closeBtn1 = CAButton::createWithFrame(closeNode->getBounds(), CAButtonTypeCustom);
	closeBtn1->addTarget(this, CAControl_selector(GDShareView::onCloseBtnClick), CAControlEventTouchUpInSide);
	closeNode->addSubview(closeBtn1);

	CALabel *closeLabel = GDUtil::createLabelWithFrame(closeBtn1->getBounds(), "取消", GD_COLOR_323232, _ds(28), "", CATextAlignmentCenter, CAVerticalTextAlignmentCenter);
	closeBtn1->addSubview(closeLabel);

	GDUtil::setViewHeight(mainPanel, closeNode); // 设置主面板和底部一样高

	CCRect rect = mainPanel->getFrame();
	rect.origin.y = winSize.height - rect.size.height;
	mainPanel->setFrame(rect);
}

void GDShareView::initSharePlatform(){
	GD_ConfigDataModule_getInstance

	int nodeWidth = mainNode->getBounds().size.width;
	int sharePlatformSize =	cdm->sharePlatforms.size();

	int horizontalSpace = _ds(0); // 横向间距
	int verticalSpace = _ds(0); // 竖向间距
	int itemWidth = _ds(196); // 每个宽度
	int itemHeight = _ds(186); // 每个高度
	int x = 0; // 
	int y = verticalSpace;

	if (sharePlatformSize > 0){
		GDSharePlatform item;
		for (int i = 0; i < sharePlatformSize; i++){
			item = cdm->sharePlatforms.at(i);

			CAView* itemShareView = createShareItemView(item, itemWidth, itemHeight);
			mainNode->addSubview(itemShareView);

			if (x + itemWidth + horizontalSpace > nodeWidth){ // 如果超出宽度，换行
				x = 0;
				y += (verticalSpace + itemHeight);
			}

			itemShareView->setFrame(CCRect(x, y, itemWidth, itemHeight));

			x += itemWidth + horizontalSpace;
		}
	}

	GDUtil::setViewHeightFull(mainNode); // 调整节点高度
}

CAView* GDShareView::createShareItemView(GDSharePlatform &sharePlatform, int itemWidth, int itemHeight){
	CAView *view = CAView::createWithColor(CAColor_clear);
	
	CAButton *itemBtn = CAButton::createWithCenter(CCRect(itemWidth / 2, _ds(73), _ds(90), _ds(90)),CAButtonTypeCustom);
	itemBtn->setTextTag(sharePlatform.id);
	//itemBtn->setBackGroundViewForState(CAControlStateAll, CAView::createWithColor(CAColor_blue));
	//itemBtn->setBackGroundViewForState(CAControlStateHighlighted, CAView::createWithColor(CAColor_orange));
	itemBtn->addTarget(this, CAControl_selector(GDShareView::onShareItemBtnClick), CAControlEventTouchUpInSide);
	view->addSubview(itemBtn);

	CAImageView *itemImage = CAImageView::createWithImage(CAImage::create(GD_IMAGE_PATH + sharePlatform.icon));
	itemImage->setFrame(itemBtn->getBounds());
	itemBtn->addSubview(itemImage);

	CAImageView *topImage = CAImageView::createWithImage(CAImage::create("images/kuang.png"));
	topImage->setFrame(itemBtn->getBounds());
	itemBtn->addSubview(topImage);
	
	CALabel *title = CALabel::createWithFrame(CCRect(0, itemBtn->getFrameOrigin().y + itemBtn->getBounds().size.height + _ds(6), itemWidth,_ds(34)));
	title->setFontSize(_ds(20));
	title->setColor(GD_COLOR_969696);
	title->setTextAlignment(CATextAlignmentCenter);
	title->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	title->setText(sharePlatform.name);
	view->addSubview(title);

	return view;
}

void GDShareView::onCloseBtnClick(CrossApp::CAControl *control, CrossApp::CCPoint point){
	if (closeToHidden){
		this->setVisible(false);
	}
	else{
		this->removeFromSuperview();
	}
}

void GDShareView::onShareItemBtnClick(CrossApp::CAControl *control, CrossApp::CCPoint point){
	std::string id = control->getTextTag();

	std::string shareTitle = title;
	std::string shareCnt = cnt;
	int iid = GDUtil::stringToInt(id);

	if ((titleMap.find(iid) != titleMap.end()) && (!titleMap[iid].empty() && titleMap[iid]!="")){
		shareTitle = titleMap[iid];
	}

	if ((cntMap.find(iid) != cntMap.end()) && (!cntMap[iid].empty() && cntMap[iid] != "")){
		shareCnt = cntMap[iid];
	}

	if (IS_DEBUG) CCLog("点击了%s的分享,分享标题：%s,分享内容：%s", id.c_str(), shareTitle.c_str(), shareCnt.c_str());
	if (iid == (int)cn::sharesdk::C2DXPlatTypeSinaWeibo){
		pid = id;
		title = shareTitle;
		cnt = shareCnt;
		processShortUrl();
		return;
	}
	GDUtil::share(id, shareTitle, shareCnt, description, url, imageUrl);
}

void GDShareView::setShareInfo(std::string title, const std::string cnt, const std::string description, const std::string url, const std::string imageUrl, const std::string goodsId, map<int, string> titleMap, map<int, string> cntMap){
	this->title = title;
	this->cnt = cnt;
	this->description = description;
	this->url = url;
	this->imageUrl = imageUrl;
	this->goodsId = goodsId;
	this->titleMap = titleMap;
	this->cntMap = cntMap;
}

void GDShareView::processShortUrl()
{
	
}

void GDShareView::onProcessShortUrlFinish(const HttpResponseStatus& status, const CSJson::Value& json)
{
	std::string shortUrl = json["url"].asString();
	if (IS_DEBUG) CCLog("文章短链地址：%s",shortUrl.c_str());
	GDUtil::share(pid, title, cnt, description, shortUrl, imageUrl);
}